/* This is encsrv, a collection of initialization scripts to set up encryption for a web and database server, for GNU/Debian Linux Distributions based on systemd, particularly for Debian 8.
 * 
 * Copyright (C) 2016, gabocze
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * To contact the authors of encsrv visit https://gna.org/users/gabocze
 */

angular.module ('encsrv' ['ionic', 'notes']).controller('NoteCtrl', function($scope, Notes)
{
/* controller methods
 * insertAndSave(note)
 * deleteAndPersist(index)
 * clean ()
 * select ()
 */
$scope.notes = Notes.load();

var cleanNoteModel = function(){
    $scope.currentIndex=null;
    $scope.currentNote=Notes.newNote();
}
  cleanNoteModel();
 $scope.insertAndSave= function (note) {
       // transverse notes structure
      // find the note by title or allocate a new one
      // return the modified structure
      var i;
      for (i=0; i<$scope.notes.length; i++) {
        if ($scope.notes[i].title==note.title)
          break;
      }
      $scope.notes[i] = note;
      $scope.currentNote=note;
      $scope.currentIndex=i;
      Notes.save ($scope.notes);
    };
    $scope.deleteAndPersist=function (index ) { 
        $scope.notes.splice (index, 1);
        Notes.save ($scope.notes);
        $scope.currentIndex = null;
        $scope.currentNote = Notes.newNote();
    };
    $scope.clean = cleanNoteModel;
    $scope.select = function (index) {
        $scope.currentIndex = index;
        $scope.currentNote = $scope.notes [index];
};
});  // NoteCtl controller

